package com.andy.dogcoin

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
//import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andy.adapter.MainAdapter
import com.andy.db.CoinInfo
import com.andy.db.CoinInfoDao
import com.andy.db.DBHelper
import com.andy.db.DBManager
import java.io.File

class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    private var recyclerView: RecyclerView? = null
    private var mAdapter: MainAdapter? = null
    var listData: List<CoinInfo>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
        initView()
    }


    override fun onResume() {
        super.onResume()
        Log.e(TAG, "listData.size() = " + listData!!.size)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
//        if (requestCode == AndyAddActivity.ForResult && resultCode == AndyAddActivity.ForResult_Suc) {
//            initData()
//            initView()
//        }
    }

    private fun initView() {
        recyclerView = findViewById(R.id.main_recycler_view)
        recyclerView?.layoutManager = LinearLayoutManager(this)

        mAdapter = MainAdapter(listData, this)
        mAdapter?.setOnItemClickListener (object : MainAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                var addr = listData!!.get(position).getAddr()
                CoinDetailActivity.startActivity(this@MainActivity, addr)

            }

        })
//        mAdapter?.onItemClickListener = MyAdapter.OnItemClickListener{
//
//        }

        recyclerView?.adapter = mAdapter

//        recyclerView?.addOnItemTouchListener(
//            RecyclerItemClickListener(this, recyclerView, object : RecyclerItemClickListener.OnItemClickListener {
//                override fun onItemClick(view: View, position: Int) {
//                    // 处理列表项点击事件
//                    Toast.makeText(applicationContext, "Item clicked at position $position", Toast.LENGTH_SHORT).show()
//                }
//
//                override fun onLongItemClick(view: View, position: Int) {
//                    // 处理列表项长按事件
//                    Toast.makeText(applicationContext, "Item long clicked at position $position", Toast.LENGTH_SHORT).show()
//                }
//            })
//        )

//        listView!!.onItemClickListener =
//            OnItemClickListener { adapterView, view, i, l ->
//                WebViewActivity2.start(
//                    this@MainActivity,
//                    listData!![i].getAddr(),
//                    -1
//                )
//            }
//        listView!!.onItemLongClickListener =
//            OnItemLongClickListener { adapterView, view, i, l ->
//                WebViewActivity2.start(this@MainActivity, listData!![i].getAddr(), 1)
//                false
//            }
//        listView!!.adapter = mAdapter
    }

    private fun initData() {
        var dao = CoinInfoDao(this)
        var mDBManager = DBManager.getInstance(this)
        mDBManager.beginTransaction()
        var result = false
        try {
            listData = dao.queryGroupByAddr()
            result = true
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            mDBManager.endTransaction(result)
        }
    }

    fun andy_add_activity(v: View?) {
        AndyAddActivity.Jump(this, "", "")
    }

//    var datePath  : File? = null

    fun andy_share_activity(v: View?) {
        var datePath = getDatabasePath(DBHelper.databaseName)
        val shareIntent = Intent(Intent.ACTION_SEND)
        // Android7.0版本以上使用FileProvider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val packName = application.packageName
            shareIntent.putExtra(
                Intent.EXTRA_STREAM, FileProvider
                    .getUriForFile(
                        this, "$packName.fileprovider",
                        File(datePath.getPath())
                    )
            )
        } else {
            shareIntent.putExtra(Intent.EXTRA_STREAM, File(datePath.getPath()))
        }
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        shareIntent.type = "*/*" // 此处可发送多种文件
        shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK // 这一句一定得写
        startActivity(Intent.createChooser(shareIntent, "分享文件"))
    }
}

